//角色脚本

//获取角色列表
HandyOA.findroles = function(pageno){
	if (pageno==null) pageno=1;
	var surl = '${request.contextPath}/data/sc.action';
	var request = new Request.JSON({
			url: surl,
			onComplete: function(json,text) {
				var str='';
				str += '<table width="580" border="0">';
				str += '<tr><td align=right><a href="">添加</a> <a href="">修改</a> <a href="">删除</a> <a href="">权限分配</a></td></tr>';
				str += '</table>';
				str += '<table width="580" border="0" class="list-table">';
				
				str += '<caption>'+HandyOA.pageOverview(json.overview)+'</caption>';
				
				str += '<thead><tr><th><input type="checkbox" onclick=\"HandyOA.checkall()\">编号</th><th>角色名</th><th>说明</th><th>创建该角色的用户</th></tr></thead>';
				str += '<tbody>';
				json.recorders.each(function(item, index){
    				str+= '<tr><td><input type="checkbox" name="role_" class="role11" value="'+item.id+'">'+item.id + '</td><td>'+item.name+'</td><td>'+item.note+'</td><td>'+item.username+'</td></tr>';
				});
				str += '</tbody>';					
				str += '</table>';
	
				MochaUI.updateContent({
					'element': $('role'),
					'loadMethod': 'html',
					'content': str
				});
				var zebraTables = new ZebraTable('list-table');
			}
		}).get({"scid":"roles","pageno":pageno});
}

HandyOA.checkall = function(){
	var ids = $$('#role_ input[name=role]');
	alert(ids);
	for (var i = 0; i < ids.length; i++) {
		if (ids[i].checked==true){
			ids[i].checked = false;
		}else{
			ids[i].checked = true;
		}
	} 
}

HandyOA.pageOverview = function(overview){
	var str = "";
	str+= '共'+overview.tcount+'条 '; 
	str+= '第'+overview.cpage+'/'+overview.tpage+'页 '
	str+= overview.maxline+'条/页 ';
	if (overview.cminpage>0){
    	str += '&nbsp;<span onclick=\"HandyOA.findroles(1)\" style="cursor:hand"><b>|</b>&lt;&lt;</span>&nbsp;';
    	str += '&nbsp;<span onclick=\"HandyOA.findroles(' + overview.cminpage + ')\" style="cursor:hand">&lt;&lt;</span>&nbsp;';
    }
	overview.pagenumber.each(
	    function(no, index){
			//str+= item + ' ';
			if (overview.cpage==no){
          		str += '&nbsp;<b> '+no+'</b>&nbsp;';
       		}else{ 
          		str += '&nbsp;<span onclick=\"HandyOA.findroles(' + no + ')\" style="cursor:hand"><u><b> '+no+'</span></b></u>&nbsp;';
       		} 
		 }
	);
	if (overview.cmaxpage>0){
    	str += '&nbsp;<span onclick=\"HandyOA.findroles(' + overview.cmaxpage + ')\" style="cursor:hand">&gt;&gt;</span>&nbsp;';
    	str += '&nbsp;<span onclick=\"HandyOA.findroles(' + overview.tpage + ')\" style="cursor:hand">&gt;&gt;<b>|</b></span>&nbsp;';
    }
    str += '<span style="padding: 0"> 跳转到: <input type="text" name="custompage" size="1" style="border: 1px solid #698CC3" onKeyDown="if(event.keyCode==13){ if(!isNaN(this.value) && (this.value != \'\')){ HandyOA.findroles(this.value);}else{alert(\'请输入数字!\');this.value=\'\';}}"> 页';
	return str;
}

//角色管理的窗口名：role
HandyOA.role = function(){
	new MochaUI.Window({
		id: 'role',
		title: '角色',
		content: '<img src="${request.contextPath}/resources/images/spinner.gif" width="16px" height="16px">',
		width: 610,
		height: 400
	});
	HandyOA.findroles();
}
	
if ($('roleLink')){
	$('roleLink').addEvent('click', function(e){	
		new Event(e).stop();
		HandyOA.role();
	});
}